<?php
//ob_start();
//error_reporting(-1); ini_set('display_errors', 'on');
session_start();

/*FirePHP debugging init*/
//require_once('FirePHPCore/fb.PHP');
//fb('Just logging this event.',FirePHP::LOG);
//FB::info('Starting execution.');

if(session_id() != ''){
	if(!empty($_POST['word']) && !empty($_POST['mean']) && !empty($_POST['sent'])){
		$word = $_POST['word'];
		$meaning = $_POST['mean'];
		$sentence = $_POST['sent'];
		
		if(!empty($_SESSION['username']) && !empty($_SESSION['password'])){
			$link = mysql_connect('localhost', $_SESSION['username'], $_SESSION['password']);
			if(!$link){
				echo mysql_error();
		
			}else{
				if(mysql_select_db('greprep',$link)){
				
					/*FirePhp logging*/
					//FB::log('Connection established and database selected');
			
					$res = mysql_query('CALL add_word("'.$word.'", "'.$meaning.'" ,"'.$sentence.'", @status);', $link);
			
					if(!$res){
						echo mysql_error();
					}else{
			
						/*FirePhp logging*/
						//FB::log('add_word stored procedure called successfully');
				
						$sel = mysql_query('select @status;', $link);
						if(!$sel){
							echo mysql_error();
						}else{
				
							/*FirePhp logging*/
							//FB::info('Now checking for the status returned from add_word');
					
							$val = mysql_fetch_array($sel);
							if($val["@status"] == 1){
								echo "Word successfully added.";
							}else if($val["@status"] == 0){
								echo "Word already exists.";
							}else{
								echo "Unknown Error.";
							}
						}
						mysql_free_result($sel);
					}					
				}else{
					echo "Cannot select db error.";
				}	
			}
			mysql_close($link);
		}else{
			echo "Session error";
		}	
		
	}else{
		echo "Input error.";
	}
}else { 
	echo "Session error"; 
}
?>
